package businessLogic;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import connessioni.MyConnection;

import Beans.Sede;
import Beans.Ufficio;

public class CommonInterfaceMySQL extends CommonInterfaceEmpty {
	
	private Connection con;
	private String macro;
	
	
	public CommonInterfaceMySQL(String macrocategoria) throws SQLException, ClassNotFoundException {
		con = MyConnection.getConnection();
		macro = macrocategoria;
	}
	
	
	public String[] getListaMacroCategorie() throws SQLException {
		
		Statement st = con.createStatement();
		
		ResultSet rs = st.executeQuery("SELECT Tipo FROM Partner");
		String[] toReturn = new String[rs.getRow()];
		
		for(int i = 0; rs.next(); i++)
			toReturn[i] = rs.getString("Tipo");
		
		return toReturn;
	}
	
	
	public void aggiungiSede(Sede s) throws SQLException {
		
		Statement st = con.createStatement();
		
		st.executeUpdate("INSERT INTO sedi(IDProprietario, TipoPartner, Nazione, Citta, Regione, CAP, " 
				+ "Provincia, Via, NCivico, TipoSede, PathImg) " 
				+ " VALUES(" + s.getIdProprietario() + ", " + s.getPartner().getID() + ", "
				+ "' " + s.getNazione() + "', '" + s.getCitta() + "', '" + s.getRegione() + "', "
				+ "'" + s.getCap() + "', '" + s.getProvincia() + "', '" + s.getVia() + "', " 
				+ "'" + s.getNCivico() + "', '" + s.getTipoSede() + "', '" + s.getPathImg() + ")");
		st.close();
		
		//inserire uffici!!
		Ufficio[] u = s.getUffici();
		for(int i = 0; i < u.length; i++)
			aggiungiUfficio(u[i]);
		
		st.close();
	}
	
	
	private void aggiungiUfficio(Ufficio u) throws SQLException {
		
		Statement st = con.createStatement();
		
		st.executeUpdate("INSERT INTO uffici(IDSede, Nome, IDResponsabile) "
				+ "VALUES(" + u.getSede().getId() + ", '" + u.getNome() + "', " + u.getResponsabile().getId() + ")");
		
		st.close();
	}

}
